﻿activityRole = " 执行||审批||审核||知会";
activityType = " 所有参与||其中之一";
function onActivityDrop(e) {
    var $target = $(e.dropTarget);
    var treeview = $('#activities').data('tTreeView');
    var nodeText = treeview.getItemText(e.item);

    if ($target.is("ul.ws-activities")) {
        e.preventDefault();
        $newli = $target.addListItem(null);
         var nodetext = getTreeviewSelectedItemText($('#conditions'),e);
         initialActivityLi($newli, nodetext);
    }
    return;
}
function initialActivityLi($newli, nodetext) {
    var $target = $('.ws-activities');
    $newli.val(nodetext).find('.display-text').text(nodetext);
    $newli.addHoverFunctions({
        deleteEvent: function () {
            $newli.remove();
        },
        detailEvent: function () {
            setDetailToWindowEditor($newli, initialActivityDetail($newli), $('#Window'));
        }
    });
}

function initialActivityDetail($target) {
    var data = $target.data('lidata');
    var array = new Array();


    var participantOptions = "";
    $('.ws-participants').find('li').each(function () {
        if (participantOptions == "")
            participantOptions = $(this).data('lidata').Title;
        else
            participantOptions += "||" + $(this).data('lidata').Title;
    });

    var participantvaluetext = "";
    if (data && data.Participants) {
        for (var i = 0; i < data.Participants.length; i++) {
            if (participantvaluetext == "")
                participantvaluetext = data.Participants[i].Title;
            else
                participantvaluetext += "||" + data.Participants[i].Title;
        }
    }
    ////////////////////////////////////////////////
    var activytiResultOptions = "";
    $('.ws-activityresults').find('li').each(function () {
        if (activytiResultOptions == "")
            activytiResultOptions = $(this).data('lidata').Title;
        else
            activytiResultOptions += "||" + $(this).data('lidata').Title;
    });
    var activityResults;
    var activityresultvaluetext = "";
    //因为actvity.activityresults没有序列化,需要在这里检查是否数据库中确实存在activityresult和当前activity的对应关系
    if (data != null && !data.ActivityResults) {
        $.ajax({
            url: "/Workflow/GetActivityResultsJson",
            type: 'GET',
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            data: { targetid: data.ID },
            async: false,
            success: function (result) {
                activityResults = result;
                for (var i = 0; i < result.length; i++) {
                    if (activityresultvaluetext == "")
                        activityresultvaluetext = result[i].Title;
                    else
                        activityresultvaluetext += "||" + result[i].Title;
                }
            }
        });
    }
    else {
        if (data && data.ActivityResults) {
            activityResults = data.ActivityResults;
            for (var i = 0; i < data.ActivityResults.length; i++) {
                if (activityresultvaluetext == "")
                    activityresultvaluetext = data.ActivityResults[i].Title;
                else
                    activityresultvaluetext += "||" + data.ActivityResults[i].Title;
            }
        }

    }

    array.push({ controltype: 'text', fieldname: '环节名称', fieldvalue: data == null ? null : data.ActivityName, must: true });
    array.push({ controltype: 'number', fieldname: '环节时间', fieldvalue: data == null ? null : data.ActivityDuration, must: true });
    array.push({ controltype: 'dropdownlist', fieldname: '环节类型', optionalvalue: activityRole, fieldvalue: data == null ? null : data.ActivityRole, must: true });
    array.push({ controltype: 'checkbox', fieldname: '角色条件', fieldvalue: participantvaluetext, optionalvalue: participantOptions, fieldvaluereference: ".ws-participants li", fieldvaluedata: data == null ? null : data.Participants, must: true });

    if ($('.ws-activityresults li').length>0)
        array.push({ controltype: 'checkbox', fieldname: '环节条件', fieldvalue: activityresultvaluetext, optionalvalue: activytiResultOptions, fieldvaluereference: ".ws-activityresults li", fieldvaluedata: data == null ? null : activityResults, must: false });   
    
    array.push({ controltype: 'radio', fieldname: '参与类别', fieldvalue: data == null ? null : data.PassingRule, optionalvalue: '所有参与||其中之一' });
    array.push({ controltype: 'slide', fieldname: '环节级别', fieldvalue: data == null ? 1 : data.Level, min: 1, max: 10, step: 1 });
    $target.data('av', array);

    var win = $('#Window').data('tWindow');
    var $table = $target.getTable(
                         {
                             save: function () {
                                 if ($target.attr('sucess') == 'true') {
                                     $target.find('.display-text').text(array[0].fieldvalue);
                                     $target.attr('uniquename', array[0].fieldvalue);
                                     $target.removeClass('unsave').addClass('saved');
                                     saveActivityData($target);
                                     win.close();
                                 }
                             },
                             cancel: function () {
                                 win.close();
                             }
                         });
    return $table;
}

function saveActivityData($li) {
    var av = $li.data('av');
     var lidata;
     if (av.length == 6) {
         lidata = {
             ActivityName: av[0].fieldvalue,
             ActivityDuration: av[1].fieldvalue,
             ActivityRole: av[2].fieldvalue,
             Participants: av[3].fieldvalue,
             PassingRule: av[4].fieldvalue,
             Level: av[5].fieldvalue,
             WorkflowID: workflowid,
             ID: $li.attr('id')
         }
     }
     else if (av.length == 7) {
         lidata = {
             ActivityName: av[0].fieldvalue,
             ActivityDuration: av[1].fieldvalue,
             ActivityRole: av[2].fieldvalue,
             Participants: av[3].fieldvalue,
             ActivityResults: av[4].fieldvalue,
             PassingRule: av[5].fieldvalue,
             Level: av[6].fieldvalue,
             WorkflowID: workflowid,
             ID: $li.attr('id')

         }
     }
    $li.data('lidata', lidata);
}

function getActivitiesData() {
    var array = new Array();
    var i = 0;
    $('.ws-activities').children('li').each(function () {
        var $li = $(this);
        var data = $li.data('lidata');
        data.Sequence = i;
        i++;
        array.push(data);
    });
    return array;
}

function setActivityData(data) {
    if (data.length > 0) {
        var $ul = $('.ws-activities');
        for (var i = 0; i < data.length; i++) {
            var $li = $ul.addListItem({ lidata: data[i] });
            var uniquename = data[i].ActivityName;
            $li.attr('id', data[i].ID);
            $li.attr('uniquename', uniquename)
            initialActivityLi($li, uniquename);
        }
    }
}